//
//  EmojiView.m
//  GuoBang
//
//  Created by 梧桐树 on 16/4/28.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "EmojiView.h"

@implementation EmojiView
-(instancetype)initWithFrame:(CGRect)frame{
    
    
    self = [super initWithFrame:frame];
    if (self) {
        dataArry=[Emoji allEmoji];
        
        //创建布局对象
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //        //设置单元格的尺寸
        //        flowLayout.itemSize = CGSizeMake(80, 80);
        //        //设置头视图高度
        //        flowLayout.headerReferenceSize = CGSizeMake(0, 30);
        //flowlaout的属性，横向滑动
        //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        col = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 130) collectionViewLayout:flowLayout];
        col.delegate=self;
        col.dataSource=self;
        
        [self addSubview:col];
        [col registerClass:[EmojiCell class] forCellWithReuseIdentifier:@"EmojiCell"];
        
        UIButton *send=[[UIButton alloc]initWithFrame:CGRectMake(WIDTH- 110, col.frame.size.height+5, 50, 30)];
         [send setImage:[UIImage imageNamed:@"发送@2x.png"] forState:UIControlStateNormal];
        [send addTarget:self action:@selector(sendEmoji) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:send];
        UIButton *del=[[UIButton alloc]initWithFrame:CGRectMake(WIDTH-55, col.frame.size.height+5, 50, 30)];
        [del setImage:[UIImage imageNamed:@"delete-number@2x.png"] forState:UIControlStateNormal];
        [del addTarget:self action:@selector(deleEmoji) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:del];
        self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, WIDTH, col.frame.size.height+send.frame.size.height+10);
        self.backgroundColor=[UIColor whiteColor];
        col.backgroundColor=[UIColor whiteColor];
    }
    return self;
}
-(void)deleEmoji{
    [self.delegate dele];
}
-(void)sendEmoji{

    [self.delegate send];

}
#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataArry.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    static NSString * CellIdentifier = @"FunctionCell";
    //    FunctionCell * cell = (FunctionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    //
    UINib *nib = [UINib nibWithNibName:@"EmojiCell"
                                bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"EmojiCell"];
    EmojiCell *cell = [[EmojiCell alloc]init];
    
    // Set up the reuse identifier
    cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"EmojiCell"
                                                     forIndexPath:indexPath];
    cell.title.text=[dataArry objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(40, 40);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate addEmoji:[dataArry objectAtIndex:indexPath.row]];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
